package com.example.demo.config;

import org.apache.catalina.connector.Connector;
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.stereotype.Component;

/**
 * The valid characters are defined in RFC 7230 and RFC 3986
 * Tomcat的新版本中增加了一个新特性，就是严格按照 RFC 3986规范进行访问解析，而 RFC 3986规范定义了Url中只允许包含英文字母（a-zA-Z）、数字（0-9）、-_.~4个特殊字符以及所有保留字符(RFC3986中指定了以下字符为保留字符：! * ’ ( ) ; : @ & = + $ , / ? # [ ])。
 * @author lyc
 * @date 2019/6/3.
 * 2.0 参考 http://blog.sina.com.cn/s/blog_141e8d6260102ykq5.html
 * 1.5+ https://my.oschina.net/alvinlkk/blog/1930803
 */
@Component
public class EmbeddedTomcatConfig implements WebServerFactoryCustomizer  {

    @Override
    public void customize(WebServerFactory webServerFactory) {
        TomcatServletWebServerFactory containerFactory = (TomcatServletWebServerFactory) webServerFactory;
        containerFactory.addConnectorCustomizers(new TomcatConnectorCustomizer() {
            @Override
            public void customize(Connector connector) {
                connector.setAttribute("relaxedQueryChars", "[]|{}^<>");//这里需要转义，微博上被转换了
                connector.setAttribute("relaxedPathChars", "[]|");
            }
        });
    }
}